<template>
<div class="uni-fence-map">
  <el-dialog
    :title="title"
    :fullscreen="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="名称" prop="name"
                :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="楼层" prop="floor.id" :rules="[
              {required: true, message:'楼层不能为空', trigger:'blur'}
             ]">
            <SelectTree ref="floor" placeholder="请选择楼层" :props="{
                value: 'id',             // ID字段名
                label: 'showName',         // 显示名称
                children: 'childNodes'    // 子级字段名
              }" url="/smis/floor/jfFloor/treeData" :value="inputForm.floor.id" :clearable="true" :accordion="true"
              @getValue="floorChange" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="偏离距离" prop="rethinkDis"
                :rules="[
                  {required: true, message:'偏离距离不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.rethinkDis" placeholder="请填写偏离距离"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="偏离次数" prop="rethinkMax"
                :rules="[
                  {required: true, message:'偏离次数不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.rethinkMax" placeholder="请填写偏离次数"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24" style="height: 400px;">
          <el-form-item label="区域绘制" prop="points" :rules="[
                {required: true, message:'请绘制区域', trigger:'blur'}
               ]">
            <FenceMap ref="fenceMap"></FenceMap>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import FenceMap from '@/components/jfMap/fenceMap.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          sort: '',
          floor: {
            id: ''
          },
          points: '',
          rethinkDis: '',
          rethinkMax: ''
        }
      }
    },
    components: {
      SelectTree,
      FenceMap
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建区域`
        } else if (method === 'edit') {
          this.title = '修改区域'
        } else if (method === 'view') {
          this.title = '查看区域'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/rethink/jfRethinkArea/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfRethinkArea)
              this.loading = false
              if(this.$refs.fenceMap) {
                this.$refs.fenceMap.showFloor(this.inputForm.floor.id)
                this.$refs.fenceMap.setDrawJson(this.inputForm.points)
              }
            })
          }
          else {
            if(this.$refs.fenceMap) {
              this.$refs.fenceMap.clearDraw()
            }
          }
          if(this.$refs.fenceMap) {
            this.$refs.fenceMap.resize();
          }
        })
      },
      floorChange(value) {
        if (value && value != '') {
          this.inputForm.floor.id = value;
          this.$refs.fenceMap.showFloor(value);
        }
      },
      // 表单提交
      doSubmit () {
        this.inputForm.points = JSON.stringify(this.$refs.fenceMap.getDrawJson());
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/rethink/jfRethinkArea/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
